import { defineStore } from 'pinia'
import { api, API_PREFIX, API_TSL_FRONTEND } from 'boot/axios'
import { Loading } from 'quasar'

export const usePrintLabelStore = defineStore('printLabel', {

  state: () => ({
    defaultPrinterInfo: null,
    printerChoicesInfo: [],
    wolist: [],
    unknownlist: [],
    print_date: null
  }),
  getters: {

    getDefaultPrinter: (state) => {
      const pLabel = state.defaultPrinterInfo.department + "-" + state.defaultPrinterInfo.description
      const pValue = state.defaultPrinterInfo.printId
      const defaultPrinter = {
        label: pLabel,
        value: pValue
      }
      return defaultPrinter
    },

    getPrinterChoices: (state) => {
      const printerChoices = []
      const len = state.printerChoicesInfo.length
      for (let i = 0; i < len; i++) {
        const printer = state.printerChoicesInfo[i]
        const pLabel = printer.department + "-" + printer.description
        const pValue = printer.printId
        const newPrinter = {
          label: pLabel,
          value: pValue
        }
        printerChoices.push(newPrinter)
      }
      return printerChoices
    },

    getWOList: (state) => {
      return state.wolist
    },
    getWOListCnt: (state) => {
      return state.wolist.length
    },
    getUnknownlist: (state) => {
      return state.unknownlist
    },
    getUnknownWOCnt: (state) => {
      return state.unknownlist.length
    },
    getPrintDate: (state) => {
      return state.print_date
    }
  },
  actions: {

    async getPrinterInfo (accID) {
      Loading.show()
      try {
        // const res = await api.post(API_PREFIX + '/getPrinterInfo/', { accID: accID }, { headers: {} })
        console.log('accID: ',accID)
        const res = await api.post(API_TSL_FRONTEND +'/printer'+'/getPrinterInfo/', { accID: accID }, { headers: {} })
        // console.log(res)
        const data = res.data
        const status = res.status
        console.log('data: ', data)
        console.log('status: ', status)

        this.defaultPrinterInfo = data.defaultPrinter
        this.printerChoicesInfo = data.printerChoices
        Loading.hide()
        return true
      } catch (error) {
        // if (error) throw error
        console.log(error.response)
        Loading.hide()
        return false
      }
    },

    async searchPrintLabel (formData) {
      Loading.show()
      try {
        // const res = await api.post(API_PREFIX + '/searchPrinterLabel/', formData, { headers: {'Content-Type': 'multipart/form-data'} })
        const res = await api.post(API_TSL_FRONTEND +'/printer'+ '/searchPrinterLabel/', formData, { headers: {'Content-Type': 'multipart/form-data'} })
        // console.log(res)
        const data = res.data
        const status = res.status
        
        // set woList & unknownList
        this.wolist = data.woList
        this.unknownlist = data.unknownList

        // test
        console.log('data wolist: ', this.wolist)
        console.log('data unknownlist: ', this.unknownlist)
        console.log('status: ', status)
        Loading.hide()
        return true
      } catch (error) {
        // if (error) throw error
        console.log(error.response)
        Loading.hide()
        return false
      }
    },

    async printLabel (printer, printLabelList, accID) {
      Loading.show()
        try {
          // const res = await api.post(API_PREFIX + '/printLabel/', {"printerID": printer ,"printLabelList": printLabelList}, { headers: {} })
          const res = await api.post(API_TSL_FRONTEND +'/printer'+ '/printLabel/', {"printerID": printer ,"printLabelList": printLabelList, "accID":accID}, { headers: {} })
          const data = res.data
          console.log("printLabel", res.data)
          const status = res.status
          console.log('status: ', status)
          this.print_date = data.print_date
          console.log('print_date: ', this.print_date)
          // console.log('data.status: ', data.status)
          Loading.hide()
          return data
        } catch (error) {
          // if (error) throw error
          console.log(error.response)
          Loading.hide()
          return false
        }
    }
  }
})
